/**
 * @author David Young
 */
package csab.game.sprites;

import java.awt.Rectangle;

import csab.core.Collidable;
import csab.core.Sprite;
import csab.core.TerrainCollidable;

/**
 * This is a class that represents a sprite which has the capability to
 * detect platform edges and avoid falling off the terrain.
 * @author David Young
 */
public abstract class PlatformAISprite extends Sprite {
	protected boolean movingLAndR;
	public enum Direction { LEFT, RIGHT, NONE };
	private Direction direction;
	
	public PlatformAISprite()
	{
		super();
		init();
	}
	
	public PlatformAISprite(String name)
	{
		super(name);
		init();
	}
	
	private void init()
	{
		direction = Direction.RIGHT;
		movingLAndR = false;
	}
	
	public void setMovingLAndR(boolean movingLAndR)
	{
		this.movingLAndR = movingLAndR;
	}
	
	public boolean getMovingLAndR()
	{
		return movingLAndR;
	}
	
	public Direction getDirection()
	{
		return direction;
	}
	

	/* (non-Javadoc)
	 * @see csab.core.Collidable#collides(csab.core.Collidable)
	 */
	@Override
	public boolean collides(Collidable item) {
		if(this.canCollide())
		{
			//Use a sensor to detect the ground/platform
			if(this.movingLAndR && item instanceof TerrainCollidable)
			{
				int bot = boundingbox.y + boundingbox.height;
				Rectangle sensor = new Rectangle(boundingbox.x + (boundingbox.width / 2), bot, 1, 1);
				
				boolean senseval = item.collides(sensor);
				if(senseval && !this.isAtMapEdge())
				{
					
				}
				else if((!senseval || this.isAtMapEdge()) && this.isBased())
				{
					if(direction == Direction.RIGHT)
						direction = Direction.LEFT;
					else
						direction = Direction.RIGHT;
				}
			}
			else if(!this.movingLAndR)
				direction = Direction.NONE;
			
			return super.collides(item);
		}
		else
			return false;
	}
	/*//private Sprite lastcollided;
	public boolean collides(Collidable item) {
		if(item != this && item instanceof Sprite)
		{
			Sprite temp = (Sprite)item;
			 TODO Add the codes for combat and put this
			 * method into some sort of battle sprite class and have 
			 * this + PlayerSprite extend from the class
			 
			//Some stuff
			lastcollided = temp;
		}
		return super.collides(item);
	}*/
}
